

local juece = fk.CreateSkill{
  name = "wk_heg__juece",
  tags = {Skill.Compulsory},
}

Fk:loadTranslationTable{
  ["wk_heg__juece"] = "绝策",
  [":wk_heg__juece"] = "锁定技，所有横置的角色视为处于“鏖战”状态；游戏进入“鏖战”时，你令所有角色横置。",
  ["#wk_heg__fencheng_delay"] = "焚城",
  ["@@wk_heg__juece"] = "绝策",

  ["$wk_heg__juece1"] = "哼！你走投无路了。",
  ["$wk_heg__juece2"] = "无用之人，死！",
}

local H = require "packages/ol_hegemony/util"

local function doJuece(from, to, skiphasSkillJudge)
  local room = from.room
  if room:getTag("BattleRoyalMode") then return false end
  local hasjuece = true
  if not skiphasSkillJudge then
    if table.every(room.alive_players, function (p) return not p:hasShownSkill(juece.name) end) then
      hasjuece = false
    end
  end
  if hasjuece then
    to = to and {to} or room.alive_players
    local adds = {}
    for _, p in ipairs(to) do
      if p.chained then
        table.insert(adds, p)
        room:doIndicate(from, {p})
        room:addPlayerMark(p, "@@wk_heg__juece")
        room:handleAddLoseSkills(p, "#battle_royal&", nil, false)
      else
        room:setPlayerMark(p, "@@wk_heg__juece", 0)
        room:handleAddLoseSkills(p, "-#battle_royal&", nil, false)
      end
    end
    if #adds > 0 then
      room:notifySkillInvoked(from, juece.name, "control", adds)
      from:broadcastSkillInvoke(juece.name)
    end
  else
    for _, p in ipairs(room.alive_players) do
      room:setPlayerMark(p, "@@wk_heg__juece", 0)
      room:handleAddLoseSkills(p, "-#battle_royal&", nil, false)
    end
  end
end

juece:addEffect(H.EnterBattleRoyalMode, {
  anim_type = "big",
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(juece.name) and table.find(player.room.alive_players, function (p)
      return not p.chained
    end)
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    for _, p in ipairs(room:getAlivePlayers()) do
      if not p.chained and not p.dead then
        p:setChainState(true)
      end
    end
  end,

  can_refresh = function (self, event, target, player, data)
    return player:hasShownSkill(juece.name)
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    table.forEach(room.alive_players, function (p)
      room:setPlayerMark(p, "@@wk_heg__juece", 0)
    end)
  end,
})

juece:addEffect(fk.ChainStateChanged, {
  can_refresh = function (self, event, target, player, data)
    return player:hasShownSkill(juece.name) and not player.room:getTag("BattleRoyalMode") end,
  on_refresh = function (self, event, target, player, data)
    doJuece(player, target, true)
  end,
})

juece:addEffect(fk.Death, {
  can_refresh = function (self, event, target, player, data)
    return player == target and player:hasSkill(juece.name, true, true) and not player.room:getTag("BattleRoyalMode") end,
  on_refresh = function (self, event, target, player, data)
    doJuece(player)
  end,
})

juece:addAcquireEffect(function (self, player, is_start)
  doJuece(player, nil, true)
end)

juece:addLoseEffect(function (self, player, is_death)
  doJuece(player)
end)

return juece